﻿namespace Moment
{
    public partial class MainPage : ContentPage
    {
        string? translateNumber;
        public MainPage()
        {
            InitializeComponent();
        }

        public void OnTranslate(object sender, EventArgs e)
        {
            var enterNumber = PhoneNumberText.Text;
            translateNumber = PhonewordTranslator.ToNumber(enterNumber);
            if (string.IsNullOrWhiteSpace(translateNumber))
            {
                CallButton.IsEnabled = false;
                CallButton.Text = "Call";
            }
            else
            {
                CallButton.IsEnabled = true;
                CallButton.Text = "Call " + translateNumber;
            }
        }

        async void OnCall(object sender, EventArgs e)
        {
            if ((await this.DisplayAlert("Dial a number", "Would you like to call " + translateNumber+"?", "Yes", "No")))
            {
                try
                {
                    if(PhoneDialer.Default.IsSupported&&!string.IsNullOrWhiteSpace(translateNumber))
                        PhoneDialer.Default.Open(translateNumber);
                }
                catch(ArgumentNullException)
                {
                    await DisplayAlert("Unable to dial", "Phone number was not valid.", "OK");
                }
                catch (Exception)
                {

                    await DisplayAlert("Unable to dial", "Phone dialing failed.", "Ok");
                }
            }
        }
    }
}
